/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean;

import business.PlacesBusiness;
import business.TourBusiness;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;
import javax.servlet.http.HttpSession;
import model.Hotels;
import model.Locations;
import model.PackageTour;
import model.PackageType;
import model.Places;

/**
 *
 * @author Nhang Con
 */
@ManagedBean
@RequestScoped
public class ListBean {

    private List<Hotels> lhotel;
    private List<Places> lplaces;
    private List<PackageType> lptype;
    private int locationID;
    private int placeID;
    private int hotelID;
    private int packageID1;
    private PackageTour pktour;

    public int getPackageID1() {
        return packageID1;
    }

    public void setPackageID1(int packageID1) {
        this.packageID1 = packageID1;
    }

    public PackageTour getPktour() {
        return pktour;
    }

    public void setPktour(PackageTour pktour) {
        this.pktour = pktour;
    }

    public List<Hotels> getLhotel() {
        return lhotel;
    }

    public void setLhotel(List<Hotels> lhotel) {
        this.lhotel = lhotel;
    }

    public int getHotelID() {
        return hotelID;
    }

    public void setHotelID(int hotelID) {
        this.hotelID = hotelID;
    }

    public int getLocationID() {
        return locationID;
    }

    public void setLocationID(int locationID) {
        this.locationID = locationID;
    }

    public int getPlaceID() {
        return placeID;
    }

    public void setPlaceID(int placeID) {
        this.placeID = placeID;
    }

    public List<Locations> getLlocation() {
        return llocation;
    }

    public void setLlocation(List<Locations> llocation) {
        this.llocation = llocation;
    }

    public List<Places> getLplaces() {
        return lplaces;
    }

    public void setLplaces(List<Places> lplaces) {
        this.lplaces = lplaces;
    }
    private List<Locations> llocation;
    private TourBusiness tb = new TourBusiness();

    /** Creates a new instance of ListBean */
    public ListBean() {
        int idtour = 0;
        pktour = new PackageTour();
        if (FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("id") != null) {
            idtour = Integer.parseInt(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("id").toString());
            pktour = tb.objectPkTour(idtour);
        } else {
            idtour = Integer.parseInt(FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("ID").toString());

            pktour = tb.objectPkTour(idtour);
        }
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
        session.setAttribute("ID", idtour);


    }

    public List<PackageType> getLptype() {
        return lptype;
    }

    public void setLptype(List<PackageType> lptype) {
        this.lptype = lptype;
    }
    private PlacesBusiness placesBusiness = new PlacesBusiness();

    public void placeChangeByID(ValueChangeEvent e) {
        locationID = Integer.parseInt(e.getNewValue().toString());
        lplaces = placesBusiness.listPlacesByLocalID(locationID);
        int pID = lplaces.get(0).getPlaceID();
        lhotel = tb.getListHotelByPlace(pID);

    }

    public void hotelChangeByID(ValueChangeEvent e) {
        placeID = Integer.parseInt(e.getNewValue().toString());
        lhotel = tb.getListHotelByPlace(placeID);

    }

    public String updateTour() {
        if (FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("ID").toString() != null) {
            int idtour = Integer.parseInt(FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("ID").toString());
            HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
            if (session.getAttribute("sessionFile") != null) {
                String path = FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("sessionFile").toString();
                String getfile = "images/upload/" + path;
                pktour.setImages(getfile);
            }
            if (pktour.getEdate().after(pktour.getFdate())) {
                if (tb.updatePTour(pktour)) {

                    return "managepackagetour.xhtml?pageID1=1";
                }
                return "error.xhtml";
            }
            return "error.xhtml";
        }
        return "error.xhtml";
    }
}
